﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController
    {


        [ChildActionOnly]
        public ActionResult SGEBNREND(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            Helper.GetSTime(ref date, ref time, ref stime);
            MABF.DTEND = date;
            MABF.TMEND = stime; 
            MABF.TMSTART = stime;
            return PartialView(MABF);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SGEBNREND(ABFOLGEModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                ABFGBNEND(model);
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void ABFGBNEND(ABFOLGEModel model)
        {
            foreach(ABPOSModel abp in MABF.ABPS)
            {
                //批量End/OK 
                PREPAREPM(model, abp);
                PM["QTY"] = (double)MABF.PLANQTY;
                PM["PDCTYPE"] = MABF.PDCTYPE;
                PM["KSLTYPE"] = "BATCHEND";
                PM["PDCDATE"] = MABF.DTEND;
                PM["PDCTIMES"] = MABF.TMEND;
                PM["PDCTIMET"] = MABF.TMEND;
                PM["PDCTIME"] = MABF.TMSTART;
                PM["TP"] = MABF.MKFTP;
                VHP.VABP.SHIFT(PM);
                STOPAM();
                VHP.VABP.PDC(PM);
            }
           
            StartNewJob();
        }
        public void PREPAREPM(ABFOLGEModel model, ABPOSModel abp)
        {
            PM["LO"] = MABF.MAM.MLO;
            PM["LB"] = MABF.MAM.MLB;
            PM["LP"] = MABF.MAM.MLP;
            PM["PDCTYPE"] = MABF.PDCTYPE;
            PM["PROG"] = MABF.PROG;
            PM["KRS"] = abp.ABPAUF.Substring(0,2);
            PM["LFD"] = abp.ABPAUF.Substring(2, 6);
            PM["POS"] = abp.ABPAUF.Substring(8, 3);
            PM["STEP"] = MABF.FSTEP;
            PM["PLANQTY"] =abp.PLANQTY;
            PM["QTY"] = MABF.QTY;
            PM["KKRS"] = MABF.MAM.KKRS;
            PM["KKST"] = MABF.MAM.KKST;
            PM["MKRS"] = MABF.MAM.MKRS;
            PM["MLFD"] = MABF.MAM.MLFD;
            PM["MPOS"] = MABF.MAM.MPOS;
            PM["ABFME"] = MABF.ABFME;

            PM["MACHINEID"] = MABF.MACHINEID;
            PM["PDCSTAFF"] = MABF.STAFF;
            PM["NEWUSER"] = CUSER.PadRight(8);

            PM["ABFAGANG"] = Helper.ConvertGB2312To8859P1(MABF.ABFAGANG).PadRight(15);
            PM["ABFFIX"] = MABF.ABFFIX;
            PM["ABFMANUELKZ"] = MABF.ABFMANUELKZ;
            PM["ABFEVARHNR"] = MABF.ABFEVARHNR;
            PM["ABFEVARUNR"] = MABF.ABFEVARUNR;
            PM["MKFTP"] = MABF.MKFTP;
            PM["AUFART"] = abp.AUFART;
            PM["WAKZ"] = abp.WAKZ;
            PM["ART"] = abp.ART;
            PM["VAR"] = abp.VAR;
            PM["DESS"] = abp.DESS;
            PM["COLORIT"] = abp.COLORIT;
            PM["FARBE"] = abp.FARBE;
            PM["BESCHICH"] = abp.BESCHICH.PadRight(10);

        }
    }
}
